package com.aftersale.demo.controller;

import com.aftersale.demo.entity.*;
import com.aftersale.demo.mapper.ProjectMapper;
import com.aftersale.demo.service.ProjectService;
import com.aftersale.demo.utils.ValidGroup;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author 空
 * @date 2023/11/14 16:09
 * @desc
 */
@RestController
@RequestMapping("/project")
@Api(value="售后服务管理模块")
public class ProjectController {
    @Autowired
    ProjectService projectService;
    @Autowired
    ProjectMapper projectMapper;

    @GetMapping("/getpageproject")
    @ApiOperation(value = "项目分页查看接口")
    @PreAuthorize("hasAuthority('project_select')")
    public Result pageProjects(@RequestParam @NotBlank(message = "分页数据不能为空") Integer pageNum,
                           @RequestParam @NotBlank(message = "分页数据不能为空") Integer pageSize){
        Page<Project> projectPage=projectService.page(new Page<>(pageNum,pageSize));
        return  Result.success(projectPage);
    }

    @GetMapping("/selectprojects")
    @ApiOperation(value = "项目查询接口")
    @PreAuthorize("hasAuthority('project_select')")
    public Result selectProjects(@RequestParam @NotBlank(message = "项目名不能为空") String name,
                                @RequestParam @NotBlank(message = "分页数据不能为空") Integer pageNum,
                               @RequestParam @NotBlank(message = "分页数据不能为空") Integer pageSize){

        return  projectService.selectProjects(name,pageNum,pageSize);
    }

    @GetMapping("/getproject")
    @ApiOperation(value = "项目查看接口")
    @PreAuthorize("hasAuthority('project_select')")
    public Result projects(){
        List<Project> projectList=projectMapper.selectList(null);
        return  Result.success(projectList);
    }

    @DbLog(operation = "项目",type = LogType.ADD)
    @PostMapping("/addproject")
    @ApiOperation(value = "项目添加接口")
    @PreAuthorize("hasAuthority('project_create')")
    public Result addproject(@RequestBody @Validated({ValidGroup.ProjectName.class,ValidGroup.Version.class}) Project project){
        return projectService.addproject(project);
    }

    @DbLog(operation = "项目",type = LogType.DELETE)
    @DeleteMapping("/deleteproject")
    @PreAuthorize("hasAuthority('project_delete')")
    @ApiOperation(value = "项目删除接口")
    public Result deleteProject(@RequestParam int projectId){
        return projectService.deleteProject(projectId);
    }

    @DbLog(operation = "项目",type = LogType.UPDATE)
    @PutMapping("/updateproject")
    @PreAuthorize("hasAuthority('project_update')")
    @ApiOperation(value = "项目修改接口")
    public Result updateProject(@RequestBody @Validated({ValidGroup.ProjectName.class,ValidGroup.Version.class}) Project project){
        return projectService.updateProject(project);
    }


}
